<template>
  <div class="common_wrap">
    <div class="common_title">
      {{ id != undefined ? "编辑" : "新增" }}
      <div class="back_btn" @click="goBack"><i></i><span>返回</span></div>
    </div>
    <el-form
      :model="$data"
      ref="dynamicValidateForm"
      label-width="100px"
      class="demo-dynamic"
    >
      <el-form-item
        prop="content"
        label="通知内容："
        :rules="[
          { required: true, message: '请输入通知内容', trigger: 'blur' },
        ]"
      >
        <el-input
          v-model="content"
          placeholder="请输入通知内容"
          maxlength="300"
          style="width: 400px"
          type="textarea"
        />
      </el-form-item>
      <el-form-item
        prop="empList"
        label="提醒人员："
        :rules="[
          { required: true, message: '选择提醒人员', trigger: 'change' },
        ]"
      >
        <el-select
          v-model="empList"
          filterable
          clearable
          multiple
          style="width: 400px"
        >
          <el-option
            v-for="(item, index) in personList"
            :key="item.empId + index"
            :label="item.name"
            :value="item.empId"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('dynamicValidateForm')"
          >提交</el-button
        >
        <el-button @click="goBack">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "personMaintainIndex",
  data() {
    return {
      id: "",
      empList: [],
      content: "",
      type: "0",
      personList: [],
      empId: "",
    };
  },
  created() {
    this.getPersonList();
    // 编辑   获取对应参数
    const query = this.$route.query;
    this.id = query.id;
    console.log(this.id);
    if (this.id) {
      this.$api.noticeDetail({ id: this.id }).then((res) => {
        if (res.status === 200) {
          this.content = res.data.content;
          this.empList = res.data.empList.map((v) => Number(v.empId));
        }
      });
    }
  },
  methods: {
    goBack() {
      history.go(-1);
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.id) {
            // 编辑
            const params = {
              id: this.id,
              content: this.content,
              emps: this.empList.join(","),
            };
            this.$api.noticeEdit(params).then((res) => {
              if (res.data.errno === 200) {
                this.$message.success("提交成功");
                setTimeout(() => {
                  history.go(-1);
                }, 1500);
              } else {
                this.$message.error("提交失败");
              }
            });
          } else {
            // 新增
            const params = {
              content: this.content,
              emps: this.empList.join(","),
            };
            this.$api.noticeAdd(params).then((res) => {
              if (res.data.errno === 200) {
                this.$message.success("提交成功");
                setTimeout(() => {
                  history.go(-1);
                }, 1500);
              } else {
                this.$message.error("提交失败");
              }
            });
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 获取下拉的列表
    getPersonList() {
      // http
      this.$api.roleAllList({  limit: 99999 }).then((res) => {
        if (res.status === 200) {
          console.log(1);
          this.personList = res.data.data.items;
        }
      });
    },
  },
};
</script>

<style scoped>
.select_btn {
  color: #66b1ff;
  cursor: pointer;
}
.select_btn.disabled {
  color: #c0c4cc;
}
</style>